/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;

/**
 * @author STORM
 */
public class MIDPCanvas extends GameCanvas implements CommandListener, Runnable {

    /**
     * constructor
     */
    private LayerManager mLaymerManager;
    private TiledLayer mBackground;
    private TiledLayer mGround;
    private TiledLayer mStone;
    private TiledLayer mBrick;
    private TiledLayer mTree;
    private TiledLayer mWater;
    private TiledLayer mSnow;
    private TiledLayer mStar;
    private Sprite mTank;
    private Sprite mShell;
    private int mLevel = 1;
    private int mLives = 0;
    private int mScore = 0;
    private int x;
    private int y;
    private int i = 0;
    private int mRotate;
    private int mSpeed = 2;
    private int mMove;
    private int mStartframe;
    private String ks = new String("up");
    private boolean isTankAlive = true;
    private Player mPlayer;

    public MIDPCanvas() {
        super(true);
        setFullScreenMode(true);
        Thread t = new Thread(this);
        t.start();

        Graphics g = getGraphics();
        int w = getWidth();
        int h = getHeight();
        InputStream in = getClass().getResourceAsStream("/sounds/1.mid");
        try {
            mPlayer = Manager.createPlayer(in, "audio/midi");
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (MediaException ex) {
            ex.printStackTrace();
        }

        mLaymerManager = new LayerManager();

        Tree(g);
        Stone(g);
        Brick(g);
        Tank(g);
//        Water(g);
        Star(g);
        Ground(g);
        Win(g);
    }

    public static Image Resize(Image imgOldImage, int iNewWidth, int iNewHeight) {
        Image imgNewImage = null;
        final int iOldWidth = imgOldImage.getWidth();
        final int iOldHeight = imgOldImage.getHeight();
        int iOldRGBArray[] = new int[iOldWidth * iOldHeight];

        imgOldImage.getRGB(iOldRGBArray, 0, iOldWidth, 0, 0, iOldWidth, iOldHeight);

        int iNewRGBArray[] = new int[iNewWidth * iNewHeight];
        for (int yy = 0; yy < iNewHeight; yy++) {
            int dy = yy * iOldHeight / iNewHeight;

            for (int xx = 0; xx < iNewWidth; xx++) {
                int dx = xx * iOldWidth / iNewWidth;

                iNewRGBArray[(iNewWidth * yy) + xx] = iOldRGBArray[(iOldWidth * dy) + dx];
            }
        }
        imgNewImage = Image.createRGBImage(iNewRGBArray, iNewWidth, iNewHeight, true);
        return imgNewImage;
    }

    public void Win(Graphics g) {
        int w = this.getWidth();
        int h = this.getHeight();
        try {
            //Ve anh nen
            Image gamebg = Image.createImage("/images/gamebg.png");
            gamebg = Resize(gamebg, w, gamebg.getHeight());
            g.drawImage(gamebg, 0, 0, Graphics.TOP | Graphics.LEFT);
            g.drawImage(gamebg, 0, gamebg.getHeight(), Graphics.TOP | Graphics.LEFT);
            g.drawImage(gamebg, 0, 2 * gamebg.getHeight(), Graphics.TOP | Graphics.LEFT);
            g.drawImage(gamebg, 0, 3 * gamebg.getHeight(), Graphics.TOP | Graphics.LEFT);
            g.drawImage(gamebg, 0, 4 * gamebg.getHeight(), Graphics.TOP | Graphics.LEFT);
            g.drawImage(gamebg, 0, 5 * gamebg.getHeight(), Graphics.TOP | Graphics.LEFT);
            g.drawImage(gamebg, 0, 6 * gamebg.getHeight(), Graphics.TOP | Graphics.LEFT);
            g.drawImage(gamebg, 0, 7 * gamebg.getHeight(), Graphics.TOP | Graphics.LEFT);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void BackGroud(Graphics g) {
        try {
            int w = this.getWidth();
            int h = this.getHeight();

//Ve anh hai goc tren man hinh
            Image gameconer = Image.createImage("/images/gameconer.png");
            g.drawImage(gameconer, 0, 0, Graphics.TOP | Graphics.LEFT);
            g.drawRegion(gameconer, 0, 0, 25, 21, Sprite.TRANS_MIRROR, w, 0, Graphics.TOP | Graphics.RIGHT);
//Anh command
            Image btnleft = Image.createImage("/images/btnleft.png");
            g.drawImage(btnleft, 0, h, Graphics.BOTTOM | Graphics.LEFT);

            Image btnright = Image.createImage("/images/btnright.png");
            g.drawImage(btnright, w, h, Graphics.BOTTOM | Graphics.RIGHT);
//Hien thi Game Level
            Image gamelevel = Image.createImage("/images/gamelevel.png");
            g.drawImage(gamelevel, w / 2, 0, Graphics.TOP | Graphics.HCENTER);

            g.setColor(0xffffff);
            Font font = g.getFont();
            font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
            g.setFont(font);
            String mSLevel = "Level " + mLevel;
            g.drawString(mSLevel, w / 2, 0, Graphics.TOP | Graphics.HCENTER);

            g.drawString("Exit", 10, h - 3, Graphics.BOTTOM | Graphics.LEFT);
            g.drawString("Pause", w - 10, h - 3, Graphics.BOTTOM | Graphics.RIGHT);

            // btnleft.getHeight() = 24
            // gamelevel.getHeight() = 20

            g.setColor(0xb5afc0);

//Ve bo cuc theo kich thuoc man hinh

            if (w > 260 && h > (260 + 67)) {
                g.drawRect((w - 260) / 2 - 1, ((h - 24 - 260) / 2) - 1, 261, 261);
                int z = ((h - 24 - 260) / 2) + 260 + 2;
                if (w > 310) {
                    Image gamelives = Image.createImage("/images/gamelives.png");
                    g.drawImage(gamelives, 0, h - ((h - 260) / 2) - (24 / 2) + 2, Graphics.TOP | Graphics.HCENTER);

                    Image tanklives = Image.createImage("/images/tank.png");
                    g.drawRegion(tanklives, 0, 0, 16, 16, Sprite.TRANS_NONE, 5, h - ((h - 260) / 2) - (24 / 2) + 2 + (gamelives.getHeight() / 2), Graphics.VCENTER | Graphics.LEFT);

                    Image gamescore = Image.createImage("/images/gamescore.png");
                    g.drawImage(gamescore, gamelives.getWidth() / 2, h - ((h - 260) / 2) - (24 / 2) + 2, Graphics.TOP | Graphics.LEFT);

                    Image gamepanel = Image.createImage("/images/gamepanel.png");
                    g.drawImage(gamepanel, w, h - ((h - 260) / 2) - (24 / 2) + 2, Graphics.TOP | Graphics.RIGHT);

                    g.setColor(0xffffff);

                    g.drawString("" + mLives, 25, z + 4 + (gamelives.getHeight() / 2) + 2, Graphics.BASELINE | Graphics.LEFT);
                    g.drawString("" + mScore, w / 2 + gamescore.getWidth() / 2 - 5, z + 5 + (gamescore.getHeight() / 2) + 2, Graphics.BASELINE | Graphics.RIGHT);

                } else if (w < 310) {
                    Image gamelives = Image.createImage("/images/gamelives.png");
                    g.drawImage(gamelives, 0, z, Graphics.TOP | Graphics.HCENTER);

                    Image tanklives = Image.createImage("/images/tank.png");
                    g.drawRegion(tanklives, 0, 0, 16, 16, Sprite.TRANS_NONE, 5, z + (gamelives.getHeight() / 2), Graphics.VCENTER | Graphics.LEFT);

                    Image gamescore = Image.createImage("/images/gamescore.png");
                    g.drawImage(gamescore, gamelives.getWidth() / 2, z, Graphics.TOP | Graphics.LEFT);

                    Image gamepanel = Image.createImage("/images/gamepanel.png");
                    g.drawImage(gamepanel, gamelives.getWidth() / 2 + gamescore.getWidth(), z, Graphics.TOP | Graphics.LEFT);

                    g.setColor(0xffffff);

                    g.drawString("" + mLives, 25, z + 4 + (gamelives.getHeight() / 2) + 2, Graphics.BASELINE | Graphics.LEFT);
                    g.drawString("" + mScore, w / 2 + gamescore.getWidth() / 2 - 5, z + 5 + (gamescore.getHeight() / 2) + 2, Graphics.BASELINE | Graphics.RIGHT);
                }
            } else if (w < 260 && h > (260 + 67)) {
//                g.setClip(0, (h - btnleft.getHeight() - 260) / 2, w, 260);
                g.drawRect(0, (h - 24 - 260) / 2, w, 260);
                int z = ((h - 24 - 260) / 2) + 260 + 2;
                Image gamelives = Image.createImage("/images/gamelives.png");
                g.drawImage(gamelives, 0, z, Graphics.TOP | Graphics.HCENTER);

                Image tanklives = Image.createImage("/images/tank.png");
                g.drawRegion(tanklives, 0, 0, 16, 16, Sprite.TRANS_NONE, 5, z + (gamelives.getHeight() / 2), Graphics.VCENTER | Graphics.LEFT);

                Image gamescore = Image.createImage("/images/gamescore.png");
                g.drawImage(gamescore, gamelives.getWidth() / 2, z, Graphics.TOP | Graphics.LEFT);

                Image gamepanel = Image.createImage("/images/gamepanel.png");
                g.drawImage(gamepanel, gamelives.getWidth() / 2 + gamescore.getWidth(), z, Graphics.TOP | Graphics.LEFT);

                g.setColor(0xffffff);

                g.drawString("" + mLives, 25, z + 4 + (gamelives.getHeight() / 2) + 2, Graphics.BASELINE | Graphics.LEFT);
                g.drawString("" + mScore, w / 2 + gamescore.getWidth() / 2 - 5, z + 5 + (gamescore.getHeight() / 2) + 2, Graphics.BASELINE | Graphics.RIGHT);
            } else if (w < 260 && h < (260 + 67)) {
                g.drawRect(0, 19, w - 1, h - 66);
                int z = 20 + (h - 67) + 2;
                Image gamelives = Image.createImage("/images/gamelives.png");
                g.drawImage(gamelives, 0, z, Graphics.TOP | Graphics.HCENTER);

                Image tanklives = Image.createImage("/images/tank.png");
                g.drawRegion(tanklives, 0, 0, 16, 16, Sprite.TRANS_NONE, 5, z + (gamelives.getHeight() / 2), Graphics.VCENTER | Graphics.LEFT);

                Image gamescore = Image.createImage("/images/gamescore.png");
                g.drawImage(gamescore, gamelives.getWidth() / 2, z, Graphics.TOP | Graphics.LEFT);

                Image gamepanel = Image.createImage("/images/gamepanel.png");
                g.drawImage(gamepanel, gamelives.getWidth() / 2 + gamescore.getWidth(), z, Graphics.TOP | Graphics.LEFT);

                g.setColor(0xffffff);

                g.drawString("" + mLives, 25, z + 4 + (gamelives.getHeight() / 2) + 2, Graphics.BASELINE | Graphics.LEFT);
                g.drawString("" + mScore, (gamelives.getWidth() / 2) + gamescore.getWidth() - 3, z + 5 + (gamescore.getHeight() / 2), Graphics.BASELINE | Graphics.RIGHT);

            } else if (w > 260 && h < (260 + 67)) {
                g.drawRect((w - 260) / 2 - 1, 20 - 1, 261, h - 67 + 1);
                int z = 20 + (h - 67) + 2;
                if (w >= 310) {
                    Image gamelives = Image.createImage("/images/gamelives.png");
                    g.drawImage(gamelives, 0, z, Graphics.TOP | Graphics.HCENTER);

                    Image tanklives = Image.createImage("/images/tank.png");
                    g.drawRegion(tanklives, 0, 0, 16, 16, Sprite.TRANS_NONE, 5, z + (gamelives.getHeight() / 2), Graphics.VCENTER | Graphics.LEFT);

                    Image gamescore = Image.createImage("/images/gamescore.png");
                    g.drawImage(gamescore, gamelives.getWidth() / 2, z, Graphics.TOP | Graphics.LEFT);

                    Image gamepanel = Image.createImage("/images/gamepanel.png");
                    g.drawImage(gamepanel, w, z, Graphics.TOP | Graphics.RIGHT);

                    g.setColor(0xffffff);

                    g.drawString("" + mLives, 25, z + 4 + (gamelives.getHeight() / 2) + 2, Graphics.BASELINE | Graphics.LEFT);
                    g.drawString("" + mScore, w / 2 + gamescore.getWidth() / 2 - 5, h - ((h - 260) / 2) - (24 / 2) + 5 + (gamescore.getHeight() / 2) + 2, Graphics.BASELINE | Graphics.RIGHT);
                } else if (w < 310) {
                    Image gamelives = Image.createImage("/images/gamelives.png");
                    g.drawImage(gamelives, 0, z, Graphics.TOP | Graphics.HCENTER);

                    Image tanklives = Image.createImage("/images/tank.png");
                    g.drawRegion(tanklives, 0, 0, 16, 16, Sprite.TRANS_NONE, 5, z + (gamelives.getHeight() / 2), Graphics.VCENTER | Graphics.LEFT);

                    Image gamescore = Image.createImage("/images/gamescore.png");
                    g.drawImage(gamescore, gamelives.getWidth() / 2, z, Graphics.TOP | Graphics.LEFT);

                    Image gamepanel = Image.createImage("/images/gamepanel.png");
                    g.drawImage(gamepanel, gamelives.getWidth() / 2 + gamescore.getWidth(), z, Graphics.TOP | Graphics.LEFT);

                    g.setColor(0xffffff);

                    g.drawString("" + mLives, 25, z + 4 + (gamelives.getHeight() / 2) + 2, Graphics.BASELINE | Graphics.LEFT);
                    g.drawString("" + mScore, w / 2 + gamescore.getWidth() / 2 - 5, z + 5 + (gamescore.getHeight() / 2) + 2, Graphics.BASELINE | Graphics.RIGHT);

                }
            }

//Tao Clip chua game ben trong

            if (w > 260 && h > (260 + 67)) {
                g.setClip((w - 260) / 2, (h - btnleft.getHeight() - 260) / 2, 260, 260);
            } else if (w < 260 && h > (260 + 67)) {
                g.setClip(0, (h - btnleft.getHeight() - 260) / 2, w, 260);
            } else if (w < 260 && h < (260 + 67)) {
                g.setClip(1, gamelevel.getHeight(), w - 2, h - 67);
            } else if (w > 260 && h < (260 + 67)) {
                g.setClip((w - 260) / 2, gamelevel.getHeight(), 260, h - 67);
            }

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private void Star(Graphics g) {
        try {
            Image image = Image.createImage("/images/map_image_big.png");
            mStar = new TiledLayer(26, 26, image, 10, 10);
            int[] map = {
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
            };
            for (int i = 0; i < map.length; i++) {
                int column = i % 26;
                int row = (i - column) / 26;
                mStar.setCell(column, row, map[i]);
            }
            mLaymerManager.append(mStar);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private void Tree(Graphics g) {
        try {
            Image image = Image.createImage("/images/map_image_big.png");
            mTree = new TiledLayer(26, 26, image, 10, 10);
            int[] map = {
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                33, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 33, 0, 0, 0, 0,
                33, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 33, 0, 0, 0, 0,
                33, 33, 33, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 33, 0, 0, 0, 0,
                33, 33, 33, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 33, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 33, 33, 33, 33, 33, 33, 0, 0, 0, 0, 0, 0, 33, 33, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 33, 33, 33, 33, 33, 33, 0, 0, 0, 0, 0, 0, 33, 33, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 33, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 33, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
            };
            for (int i = 0; i < map.length; i++) {
                int column = i % 26;
                int row = (i - column) / 26;
                mTree.setCell(column, row, map[i]);
            }
            mLaymerManager.append(mTree);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private void Stone(Graphics g) {
        try {
            Image image = Image.createImage("/images/map_image_big.png");
            mStone = new TiledLayer(26, 26, image, 10, 10);
            int[] map = {
                0, 0, 0, 0, 0, 0, 10, 10, 0, 0, 0, 0, 0, 0, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 10, 10, 0, 0, 0, 0, 0, 0, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                10, 10, 0, 0, 0, 0, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                10, 10, 0, 0, 0, 0, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
            };
            for (int i = 0; i < map.length; i++) {
                int column = i % 26;
                int row = (i - column) / 26;
                mStone.setCell(column, row, map[i]);
            }
            mLaymerManager.append(mStone);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private void Brick(Graphics g) {
        try {
            Image image = Image.createImage("/images/map_image_big.png");
            mBrick = new TiledLayer(26, 26, image, 10, 10);
            int[] map = {
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
                0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
                0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
                0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
                0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
                0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
                0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
                0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
                0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
                0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
                0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
                0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
                0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
                0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0,
                0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0
            };
            for (int i = 0; i < map.length; i++) {
                int column = i % 26;
                int row = (i - column) / 26;
                mBrick.setCell(column, row, map[i]);
            }
            mLaymerManager.append(mBrick);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private void Ground(Graphics g) {
        try {
            Image image = Image.createImage("/images/ground_big.png");
            mGround = new TiledLayer(26, 26, image, 10, 10);
            int[] map = {
                1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2,
                3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4,
                1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2,
                3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4,
                1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2,
                3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4,
                1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2,
                3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4,
                1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2,
                3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4,
                1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2,
                3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4,
                1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2,
                3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4,
                1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2,
                3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4,
                1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2,
                3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4,
                1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2,
                3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4,
                1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2,
                3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4,
                1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2,
                3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4,
                1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2,
                3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4
            };
            for (int i = 0; i < map.length; i++) {
                int column = i % 26;
                int row = (i - column) / 26;
                mGround.setCell(column, row, map[i]);
            }
            mLaymerManager.append(mGround);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private void Tank(Graphics g) {
        try {
            Image image = Image.createImage("/images/tank_big.png");
            mTank = new Sprite(image, 20, 20);
            mTank.setPosition(getWidth() / 2 - 40, 20 + (getHeight() - 98));
            mTank.defineReferencePixel(10, 10);
            mLaymerManager.append(mTank);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

//    private void Shell(Graphics g) {
//        
//    }
    public void run() {
        while (true) {
            try {
                mPlayer.start();
            } catch (MediaException ex) {
                ex.printStackTrace();
            }
            try {
                int w = getWidth();
                int h = getHeight();

// Build ra dien thoai bao loi "null pointerjava/lang/NullPointerException", bo di thi binh thuong
                int ox = mTank.getX();
                int oy = mTank.getY();

                int key = getKeyStates();

                if ((key & RIGHT_PRESSED) != 0) {
                    ks = "right";
                    mTank.setTransform(Sprite.TRANS_ROT90);
                    mTank.setFrame(i++);
                    if (i > 1) {
                        i = 0;
                    }
                    mTank.setPosition(ox + mSpeed, oy);
                    if (ox > w / 2) {
                        mTank.setPosition(ox, oy);
                        mTree.move(-mSpeed, 0);
                        mStone.move(-mSpeed, 0);
                        mBrick.move(-mSpeed, 0);
                        mStar.move(-mSpeed, 0);
                        mGround.move(-mSpeed, 0);
                    }
                } else if ((key & LEFT_PRESSED) != 0) {
                    ks = "left";
                    mTank.setTransform(Sprite.TRANS_NONE);
                    mTank.setTransform(Sprite.TRANS_ROT270);
                    mTank.setFrame(i++);
                    if (i > 1) {
                        i = 0;
                    }
                    mTank.setPosition(ox - mSpeed, oy);
                    if (ox < w / 2) {
                        mTank.setPosition(ox, oy);
                        mTree.move(mSpeed, 0);
                        mStone.move(mSpeed, 0);
                        mBrick.move(mSpeed, 0);
                        mStar.move(mSpeed, 0);
                        mGround.move(mSpeed, 0);
                    }
                } else if ((key & UP_PRESSED) != 0) {
                    ks = "up";
                    mTank.setTransform(Sprite.TRANS_NONE);
                    mTank.setFrame(i++);
                    if (i > 1) {
                        i = 0;
                    }
                    mTank.setPosition(ox, oy - mSpeed);
                    if (oy < (20 + (h - 67)) / 2) {
                        mTank.setPosition(ox, oy);
                        mTree.move(0, mSpeed);
                        mStone.move(0, mSpeed);
                        mBrick.move(0, mSpeed);
                        mStar.move(0, mSpeed);
                        mGround.move(0, mSpeed);
                    }
                } else if ((key & DOWN_PRESSED) != 0) {
                    ks = "down";
                    mTank.setTransform(Sprite.TRANS_NONE);
                    mTank.setTransform(Sprite.TRANS_ROT180);
                    mTank.setFrame(i++);
                    if (i > 1) {
                        i = 0;
                    }
                    mTank.setPosition(ox, oy + mSpeed);
                    if (oy < (20 + (h - 67)) / 2) {
                        mTank.setPosition(ox, oy);
                        mTree.move(0, -mSpeed);
                        mStone.move(0, -mSpeed);
                        mBrick.move(0, -mSpeed);
                        mStar.move(0, -mSpeed);
                        mGround.move(0, -mSpeed);
                    }
                } else if ((key & FIRE_PRESSED) != 0) {
                    if (ks == "up") {
                        try {
                            Image image = Image.createImage("/images/shell_big.png");
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                }

//Kiem tra va cham voi da + gach + nuoc

                if (mTank.collidesWith(mBrick, true)) {
                    mTank.setPosition(ox, oy);
                    mTree.move(0, 0);
                    mStone.move(0, 0);
                    mBrick.move(0, 0);
                    mStar.move(0, 0);
                    mGround.move(0, 0);
                }

                if (mTank.collidesWith(mStone, true)) {
                    mTank.setPosition(ox, oy);
                    mTree.move(0, 0);
                    mStone.move(0, 0);
                    mBrick.move(0, 0);
                    mStar.move(0, 0);
                    mGround.move(0, 0);
                }
//                if (mTank.collidesWith(mWater, true)) {
//                    mTank.setPosition(ox, oy);
//                }

                Graphics g = getGraphics();
                BackGroud(g);
                mLaymerManager.paint(g, (w - 260) / 2, 20 + h - 67 - 260);
                flushGraphics();
                Thread.currentThread().sleep(50);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }

    }

    /**
     * Called when action should be handled
     */
    public void commandAction(Command command, Displayable displayable) {
    }
}